package com.school.vaccination.controller;

import com.github.pagehelper.PageInfo;
import com.school.support.base.BaseController;
import com.school.vaccination.domain.Inventory;
import com.school.vaccination.domain.Vaccine;
import com.school.vaccination.model.InventoryModel;
import com.school.vaccination.service.InventoryService;
import com.school.vaccination.service.VaccineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by 执笔 on 2017/3/18.
 */
@Controller
@RequestMapping("inventory")
public class InventoryController extends BaseController {

    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private VaccineService vaccineService;

    /**
     * 库存列表
     *
     * @param model
     * @param inventoryModel
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, InventoryModel inventoryModel, Page page) {
        Example example = ExampleUtil.getInstance()
                .addParam("v.name", inventoryModel.getVaccineName(), ExampleType.Operation.LIKE)
                .addOrder("i.addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<InventoryModel> pageInfo = inventoryService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "inventory/list";
    }

    /**
     * 跳转到添加库存页面
     *
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        List<Vaccine> vaccineList = vaccineService.selectAll();
        model.addAttribute(vaccineList);
        return "inventory/add";
    }

    /**
     * 添加库存
     *
     * @param inventory
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Inventory inventory) {
        inventory.setAddtime(new Date());
        inventoryService.insertInventory(inventory);
        return redirect("list" );
    }

    /**
     * 库存详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Inventory inventory = inventoryService.selectByPK(id);
        if (null == inventory) {
            throw new MessageException("该库存不存在");
        }
        List<Vaccine> vaccineList = vaccineService.selectAll();
        model.addAttribute(vaccineList);
        model.addAttribute(inventory);
        return "inventory/detail";
    }

    /**
     * 更新库存信息
     *
     * @param inventory
     * @return
     */
    @RequestMapping("update")
    public String uopdate(Inventory inventory) {
        inventoryService.updateInventory(inventory);
        return redirect("list" );
    }
}
